package com.lv.phuotbacnam.bl.queries;

import java.util.List;

import com.lv.phuotbacnam.bl.callback.CallBackFromParse;
import com.lv.phuotbacnam.bl.data.BinhChonObject;
import com.lv.phuotbacnam.bl.data.DiaDiemObject;
import com.lv.phuotbacnam.bl.log.Logger;
import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseGeoPoint;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;
import com.parse.SaveCallback;

public class DiaDiemQueries {
	/**
	 * To create a new dia diem on Parse server
	 */
	public static void createNewDiaDiem(DiaDiemObject diaDiemObject,
			final CallBackFromParse<Object, ParseObject> callBack) {
		final ParseObject diadiem = new ParseObject(
				QueriesConstant.TableDiaDiem.Table_Name);
		diadiem.put(QueriesConstant.TableDiaDiem.COLUMN_ten_dia_diem,
				diaDiemObject.getTen_dia_diem());
		diadiem.put(QueriesConstant.TableDiaDiem.COLUMN_mo_ta,
				diaDiemObject.getMo_ta());
		diadiem.put(QueriesConstant.TableDiaDiem.COLUMN_list_photo,
				diaDiemObject.getList_photo());
		diadiem.put(
				QueriesConstant.TableDiaDiem.COLUMN_diem_binh_chon_nguoi_tao,
				diaDiemObject.getDiem_binh_chon());
		diadiem.put(QueriesConstant.TableDiaDiem.COLUMN_diem_binh_chon,
				diaDiemObject.getDiem_binh_chon());
		diadiem.put(QueriesConstant.TableDiaDiem.COLUMN_luot_binh_chon,
				diaDiemObject.getLuot_binh_chon());
		diadiem.put(QueriesConstant.TableDiaDiem.COLUMN_luot_visited,
				diaDiemObject.getLuot_visited());
		diadiem.put(QueriesConstant.TableDiaDiem.COLUMN_luot_xem,
				diaDiemObject.getLuot_xem());
		diadiem.put(QueriesConstant.TableDiaDiem.COLUMN_vi_tri,
				diaDiemObject.getVi_tri());
		// TODO sau nay se set enable = false de cho admin duyet
		diadiem.put(QueriesConstant.TableDiaDiem.COLUMN_enable, true);
		diadiem.put(QueriesConstant.TableDiaDiem.COLUMN_nguoi_tao,
				ParseUser.getCurrentUser());
		diadiem.saveInBackground(new SaveCallback() {
			@Override
			public void done(ParseException e) {
				if (e == null)
					callBack.done("", diadiem);
				Logger.logMessage("DiadiemObjectId: " + diadiem.getObjectId());
			}
		});
	}

	/**
	 * To create a new dia diem on Parse server
	 */
	public static void updateDiaDiem(final ParseObject diadiem,
			DiaDiemObject diaDiemObject) {
		// update binh chon table
		BinhChonObject binhChonObject = new BinhChonObject(
				diaDiemObject.getDiem_binh_chon(), "");
		BinhChonQueries.updateBinhChon(diadiem, binhChonObject, null);

		// update diadiem table
		diadiem.put(QueriesConstant.TableDiaDiem.COLUMN_ten_dia_diem,
				diaDiemObject.getTen_dia_diem());
		diadiem.put(QueriesConstant.TableDiaDiem.COLUMN_mo_ta,
				diaDiemObject.getMo_ta());
		diadiem.put(QueriesConstant.TableDiaDiem.COLUMN_list_photo,
				diaDiemObject.getList_photo());
		diadiem.put(
				QueriesConstant.TableDiaDiem.COLUMN_diem_binh_chon_nguoi_tao,
				diaDiemObject.getDiem_binh_chon());
		// diadiem.put(QueriesConstant.TableDiaDiem.COLUMN_diem_binh_chon,
		// diaDiemObject.getDiem_binh_chon());
		// diadiem.put(QueriesConstant.TableDiaDiem.COLUMN_luot_binh_chon,
		// diaDiemObject.getLuot_binh_chon());
		// diadiem.put(QueriesConstant.TableDiaDiem.COLUMN_luot_visited,
		// diaDiemObject.getLuot_visited());
		// diadiem.put(QueriesConstant.TableDiaDiem.COLUMN_luot_xem,
		// diaDiemObject.getLuot_xem());
		diadiem.put(QueriesConstant.TableDiaDiem.COLUMN_vi_tri,
				diaDiemObject.getVi_tri());
		// TODO sau nay se set enable = false de cho admin duyet
		// diadiem.put(QueriesConstant.TableDiaDiem.COLUMN_enable, true);
		// diadiem.put(QueriesConstant.TableDiaDiem.COLUMN_nguoi_tao,
		// ParseUser.getCurrentUser());
		diadiem.saveInBackground(new SaveCallback() {
			@Override
			public void done(ParseException e) {
				// if (e == null)
				// callBack.done("", diadiem);
				// Logger.logMessage("DiadiemObjectId: " +
				// diadiem.getObjectId());
			}
		});
	}

	/**
	 * Lay list dia diem duoc visited nhieu
	 */
	public static void getDiaDiemMostVisited(
			final CallBackFromParse<Object, List<ParseObject>> callBack) {
		ParseQuery<ParseObject> query = ParseQuery
				.getQuery(QueriesConstant.TableDiaDiem.Table_Name);
		query.whereEqualTo(QueriesConstant.TableDiaDiem.COLUMN_enable, true);
		query.orderByDescending(QueriesConstant.TableDiaDiem.COLUMN_luot_visited);
		query.setLimit(QueriesConstant.LIMIT);
		query.findInBackground(new FindCallback<ParseObject>() {
			@Override
			public void done(List<ParseObject> diadiem, ParseException e) {
				if (e == null)
					callBack.done("", diadiem);
			}
		});
	}

	/**
	 * Lay list dia diem duoc xem nhieu
	 */
	public static void getDiaDiemMostView(
			final CallBackFromParse<Object, List<ParseObject>> callBack) {
		ParseQuery<ParseObject> query = ParseQuery
				.getQuery(QueriesConstant.TableDiaDiem.Table_Name);
		query.whereEqualTo(QueriesConstant.TableDiaDiem.COLUMN_enable, true);
		query.orderByDescending(QueriesConstant.TableDiaDiem.COLUMN_luot_xem);
		query.setLimit(QueriesConstant.LIMIT);
		query.findInBackground(new FindCallback<ParseObject>() {
			@Override
			public void done(List<ParseObject> diadiem, ParseException e) {
				if (e == null)
					callBack.done("", diadiem);
			}
		});
	}

	/**
	 * Lay list dia diem gan nhat
	 */
	public static void getDiaDiemNearest(ParseGeoPoint currentLocation,
			final CallBackFromParse<Object, List<ParseObject>> callBack) {
		ParseQuery<ParseObject> query = ParseQuery
				.getQuery(QueriesConstant.TableDiaDiem.Table_Name);
		query.whereEqualTo(QueriesConstant.TableDiaDiem.COLUMN_enable, true);
		query.whereNear(QueriesConstant.TableDiaDiem.COLUMN_vi_tri,
				currentLocation);
		query.setLimit(QueriesConstant.LIMIT);
		query.findInBackground(new FindCallback<ParseObject>() {
			@Override
			public void done(List<ParseObject> diadiem, ParseException e) {
				if (e == null)
					callBack.done("", diadiem);
			}
		});
	}

	/**
	 * Lay list dia diem moi tao nhat
	 */
	public static void getDiaDiemNewest(
			final CallBackFromParse<Object, List<ParseObject>> callBack) {
		ParseQuery<ParseObject> query = ParseQuery
				.getQuery(QueriesConstant.TableDiaDiem.Table_Name);
		query.whereEqualTo(QueriesConstant.TableDiaDiem.COLUMN_enable, true);
		query.orderByDescending(QueriesConstant.TableDiaDiem.COLUMN_createdAt);
		query.setLimit(QueriesConstant.LIMIT);
		query.findInBackground(new FindCallback<ParseObject>() {
			@Override
			public void done(List<ParseObject> diadiem, ParseException e) {
				if (e == null)
					callBack.done("", diadiem);
			}
		});
	}
}
